package cn.desk.user.action;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.desk.application.entity.Application;
import cn.desk.commons.action.CurdAction;
import cn.desk.commons.entity.PageMode;
import cn.desk.commons.service.IBaseService;
import cn.desk.user.entity.User;
import cn.desk.user.entity.UserMenuApplication;
import cn.desk.user.service.IUserMenuApplicationService;

public class UserMenuApplicationAction<T extends UserMenuApplication> extends
		CurdAction<T> {
	private static final Logger logger = LoggerFactory
			.getLogger(UserMenuApplicationAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IUserMenuApplicationService<T> userMenuApplicationService;
	private IBaseService<Application> applicationService;
	private List<Application> applications;

	/**
	 * 查询桌面的菜单
	 * 
	 * @return
	 */
	public String loadMenuApp() {
		logger.debug("loadMenuApp start");
		User user = (User) getSessionValue(UserAction.SESSION_KEY_USER);
		list = userMenuApplicationService
				.listByUserIdAndParent(user.getId(), t);
		if (t.getMenuId() != null) {
			t.setUserId(user.getId());
			super.load();
		}

		logger.debug("loadMenuApp end");
		return "loadMenuApp";
	}

	public String listAllMenu() {
		logger.debug("listAllMenu start");
		User user = (User) getSessionValue(UserAction.SESSION_KEY_USER);
		list = userMenuApplicationService.listAllMenuByUserId(user.getId());
		logger.debug("loadMenuApp end");
		return "listAllMenu";
	}

	private void listApplication() {
		logger.debug("listApplication start");
		Application app = new Application();
		applications = applicationService.list(app);
		logger.debug("listApplication end");
	}

	@Override
	public String load() {
		try {
			logger.debug("load start");
			listAllMenu();
			listApplication();
			User user = (User) getSessionValue(UserAction.SESSION_KEY_USER);
			t.setUserId(user.getId());
			return super.load();
		} finally {
			logger.debug("load end");
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.desk.commons.action.CurdAction#listPage()
	 */
	@Override
	public String listPage() {
		if ("".equals(t.getParentId())) {
			t.setParentId(null);
		}
		return super.listPage();
	}

	@Override
	public String getModeName() {
		return "userMenuApplication";
	}

	public void setUserMenuApplicationService(
			IUserMenuApplicationService<T> userMenuApplicationService) {
		this.userMenuApplicationService = userMenuApplicationService;
	}

	@Override
	public List<T> getList() {
		return super.getList();
	}

	@Override
	public Map<String, List<String>> getFieldErrors() {
		return super.getFieldErrors();
	}

	@Override
	public Collection<String> getActionErrors() {
		return super.getActionErrors();
	}

	@Override
	public T getT() {
		return super.getT();
	}

	@Override
	public PageMode<T> getPageMode() {
		return super.getPageMode();
	}

	public void setApplicationService(
			IBaseService<Application> applicationService) {
		this.applicationService = applicationService;
	}

	public List<Application> getApplications() {
		return applications;
	}

	public void setApplications(List<Application> applications) {
		this.applications = applications;
	}
}
